﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FillCartridgeBBB.Models;

namespace FillCartridgeBBB.Controllers
{
    public class DepartmentController : Controller
    {
       private DataManager _dataManager;

       public DepartmentController(DataManager dataManager)
        {
            _dataManager = dataManager;
        }

        public ActionResult List()
        {
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();
        }

        //
        // GET: /Department/Details/5

        public ActionResult Details(int id)
        {
            ViewData.Model = _dataManager.Departmets.SelectById(id);
            return View();
        }

        //
        // GET: /Department/Create
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            ViewData["Printers"] = _dataManager.Printers.SelectAll();
            return View();
        } 

        //
        // POST: /Department/Create

        [HttpPost]
        public ActionResult Create(Department department)
        {
            if (department.Name == null)
            {
                ModelState.AddModelError("Name", "Невведено імя підрозділу");
            }

            if (department.Adress == null)
            {
                ModelState.AddModelError("Adress", "Нееведено адресу підрозділу");
            }

            if (ModelState.IsValid)
            {
                var printers = Request["PrinterId"];
                _dataManager.Departmets.Insert(department, printers);
                return RedirectToAction("List");
            }
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();

        }
        
        //
        // GET: /Department/Edit/5
 
        public ActionResult Edit(int id)
        {
            ViewData.Model = _dataManager.Departmets.SelectById(id);
            ViewData["Printers"] = _dataManager.Printers.SelectAll();
            return View();
        }

        //
        // POST: /Department/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Department department)
        {
            if (department.Name == null)
            {
                ModelState.AddModelError("Name", "Невведено імя підрозділу");
            }

            if (department.Adress == null)
            {
                ModelState.AddModelError("Adress", "Нееведено адресу підрозділу");
            }

            if (ModelState.IsValid)
            {
                department.DepartmentId = id;
                var printers = Request["PrinterId"];
                _dataManager.Departmets.Update(department, printers);
                return RedirectToAction("List");
            }
            ViewData["Departments"] = _dataManager.Departmets.SelectAll();
            return View();
        }

        //
        // GET: /Department/Delete/5
 
        public ActionResult Delete(int id)
        {
            _dataManager.Departmets.Delete(id);
            return RedirectToAction("List");
        }
    }
}
